﻿using System;
using System.Collections.Generic;
using System.Linq;
using DTO;
using System.Data;

namespace DAO
{
    public class HocSinhDAO
    {
        //Insert new HocSinh
        public static bool InsertHocSinh(HocSinhDTO hsDTO)
        {
            DataProvider d = new DataProvider();
            String strSQL = "INSERT INTO HocSinh(TenHocSinh)";
            strSQL += " VALUES (N'" + hsDTO.TenHocSinh + "')";
            d.ExecuteQuery(strSQL);
            return true;
        }

        //Delete old HocSinh
        public static bool DeleteHocSinh(int maHocSinh)
        {
            DataProvider d = new DataProvider();
            String strSQL = "DELETE FROm HocSinh";
            strSQL += " WHERE MaHocSinh = " + maHocSinh.ToString();
            d.ExecuteQuery(strSQL);
            return true;
        }

        //Upadate information for HocSinh Object
        public static bool UpdateHocSinh(HocSinhDTO hsDTO)
        {
            DataProvider d = new DataProvider();
            String strSQL = "UPDATE HocSinh SET TenHocSinh = '" + hsDTO.TenHocSinh + "' WHERE MaHocSinh = " + hsDTO.MaHocSinh.ToString();
            d.ExecuteQuery(strSQL);
            return true;
        }

        //Load list of HocSinh
        public static List<HocSinhDTO> ListHocSinhAll()
        {
            DataProvider d = new DataProvider();
            String strSQL = "SELECT * FROM HocSinh";
            DataTable dt = d.ExecuteQuery(strSQL);

            List<HocSinhDTO> lstHocSinh = new List<HocSinhDTO>();
            foreach (DataRow dr in dt.Rows)
            {
                HocSinhDTO hs = new HocSinhDTO();
                hs.MaHocSinh = (int)dr["MaHocSinh"];
                hs.TenHocSinh = dr["TenHocSinh"].ToString();

                lstHocSinh.Add(hs);
            }
            return lstHocSinh;
        }
    }
}